// 门店内搜索
const basePage = require('../../../common/BasePage')
const baseList = require('../../../common/BaseList')
Page({
  behaviors: [basePage, baseList],
  data: {
    searchValue: '',
    store: {},
    reqUrl: '/v1/goods/goods-of-stores',
    cartObj: {},
    cartNum: 0,
    reqData: {
      goodsName: ""
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      store: wx.getStorageSync('store')
    })
    this.data.reqUrl = '/v1/goods/' + this.data.store.sid + '/goods-of-stores'
    let storeSid = this.data.store.sid;
    let globalData = getApp().globalData;
    this.data.latitude = globalData.latitude;
    this.data.longitude = globalData.longitude
    this.request({
      url: this.data.host + "/v1/carts/me",
      data: {
        size: 50,
        latitude: this.data.latitude,
        longitude: this.data.longitude,
      },
    }, res => {

      let obj = {};
      let cartNum = 0;
      res.data.records.map(val => {
        val.goods.map(good => {
          cartNum += good.number * 1;
          if (good.storeSid == storeSid) {
            obj[good.goodsSid] = {
              storeSid: good.storeSid,
              number: good.number,
              stock: good.stock,
              cartSid: good.sid
            }
          }
        })
      });

      this.setData({
        cartObj: obj,
        cartNum: cartNum
      })
      this.req_items()
    })
  },
  search_click(e) {
    console.log(e);
    this.data.reqData.goodsName = e.detail.value;
    this.data.current_page = 0;
    this.setData({
      itemArr: []
    })
    this.req_items()
  },
  add_cart(e) {
    console.log('add', e.currentTarget.dataset.item);
    let item = e.currentTarget.dataset.item;
    let i = e.currentTarget.dataset.index;

    return this.req_add_cart(this.data.store.sid, item, i)

  },
  req_add_cart(storeSid, item, i) {
    this.request({
      url: this.data.host + '/v1/carts/add',
      data: {
        goodsSid: item.sid,
        storeSid: storeSid
      },
      method: 'put',
      loadingTip: '加购...'
    }, res => {

      item.storeSid = storeSid;
      item.number = 1;
      item.cartSid = res.data
      this.data.itemArr[i] = item;
      this.setData({
        itemArr: this.data.itemArr,
        cartNum: this.data.cartNum * 1 + 1
      })
    })
  },
  cart_change(e) {

    let item = e.currentTarget.dataset.item;
    let i = e.currentTarget.dataset.index;
    if (e.detail.value == 0) {
      //删除购物车
      this.request({
        url: this.data.host + "/v1/carts/del",
        method: "put",
        data: {
          sidList: [item.cartSid]
        }
      }, res => {
        let num = e.detail.value - this.data.itemArr[i].number;
        this.data.itemArr[i].number = e.detail.value;
        this.setData({
          itemArr: this.data.itemArr,
          cartNum: this.data.cartNum + num
        })
      })
    } else {
      //修改购物车数量
      this.request({
        url: this.data.host + "/v1/carts/number",
        method: "put",
        data: {
          number: e.detail.value,
          sid: item.cartSid
        }
      }, res => {
        /**增减数量 */
        let num = e.detail.value - this.data.itemArr[i].number;
        this.data.itemArr[i].number = e.detail.value;
        this.setData({
          itemArr: this.data.itemArr,
          cartNum: this.data.cartNum + num
        })

      })
    }
  },
  /**格式化列表中的数据 */
  init_val(val, index, arr) {
    val.thumb = this.get_imgUrl(val.goodsImg);
    let obj = this.data.cartObj;
    if (obj[val.sid]) {
      let good = obj[val.sid];

      val.storeSid = good.storeSid;
      val.number = good.number;
      val.stock = good.stock;
      val.cartSid = good.cartSid
    } else {
      val.storeSid = "";
      val.number = 0;
      val.stock = 999;
    }
  },
  kf_click() {
    wx.openCustomerServiceChat({
      corpId: 'ww72ede85652b45a26',
      extInfo: {
        url: this.data.store.customerUrl,
      },
      success: (res) => {
        console.log('打开成功', res);
      },
      fail: (res) => {
        console.log('打开失败', res);
        wx.showToast({
          title: '打开失败',
          icon: 'error'
        })
      },
    })
  },
  location_click() {
    wx.openLocation({
      latitude: this.data.store.latitude * 1,
      longitude: this.data.store.longitude * 1,

      fail: (res) => {
        wx.showToast({
          title: JSON.stringify(res),
        })
      },
      complete: (res) => {},
    })
  },


  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },



})